/**
  ******************************************************************************
  * @file    EEPROM_Emulation/src/eeprom.c 
  * @author  MCD Application Team
  * @version V3.1.0
  * @date    07/27/2009
  * @brief   This file provides all the EEPROM emulation firmware functions.
  ******************************************************************************
  * @copy
  *
  * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
  * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
  * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
  * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
  * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  *
  * <h2><center>&copy; COPYRIGHT 2009 STMicroelectronics</center></h2>
  */ 
/** @addtogroup EEPROM_Emulation
  * @{
  */ 

/* Includes ------------------------------------------------------------------*/

#include <gd32f10x.h>
#include "debug.h"
#include "string.h"
#include "eeprom.h"
#include "function_config.h"
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/

/* Global variable used to store variable value in read sequence */

/* Virtual address defined by the user: 0xFFFF value is prohibited */

/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/

/**
  * @brief  Restore the pages to a known good state in case of page's status
  *   corruption after a power loss.
  * @param  None.
  * @retval - Flash error code: on write Flash error
  *         - FLASH_COMPLETE: on success
  */
#ifdef EEPROM_DRIVER

 #define EEADDRESS  0x0800F800
 
#define BufferSize              1024
#define FMC_PAGE_SIZE           ((uint16_t)0x400)
#define BANK1_WRITE_START_ADDR  ((uint32_t)EEADDRESS)
#define BANK1_WRITE_END_ADDR    ((uint32_t)EEADDRESS+0x400)
  
uint16_t EE_Init(void)
{
     FMC_Unlock();
	 return 0;
}

uint16_t EE_WriteData(uint16_t VirtAddress, uint8_t *str,uint16_t len)
{
   uint32_t i;
   uint32_t temp,*srcptr;
   __IO uint32_t NbrOfPage = 0x00;
   volatile FMC_State FMCStatus = FMC_READY;
   uint32_t EraseCounter = 0x00, Address = 0x00 ;
   /* Unlock the Flash Bank1 Program Erase controller */
    FMC_Unlock();
    
    /* Define the number of page to be erased */
    NbrOfPage = (BANK1_WRITE_END_ADDR - BANK1_WRITE_START_ADDR) / FMC_PAGE_SIZE;
    
    /* Clear All pending flags */
    FMC_ClearBitState(FMC_FLAG_EOP | FMC_FLAG_WERR | FMC_FLAG_PERR );
    
    /* Erase the FLASH pages */
    for(EraseCounter = 0; EraseCounter < NbrOfPage; EraseCounter++)
    {
        FMCStatus = FMC_ErasePage(BANK1_WRITE_START_ADDR + (FMC_PAGE_SIZE * EraseCounter));
        FMC_ClearBitState(FMC_FLAG_EOP | FMC_FLAG_WERR | FMC_FLAG_PERR );
    }


    /* Program Flash Bank1 */
    Address = BANK1_WRITE_START_ADDR;
    srcptr = (uint32_t *)str;
    
    while(Address < BANK1_WRITE_END_ADDR)
    {
        temp = *srcptr++;
        FMCStatus = FMC_ProgramWord(Address, temp);
        Address = Address + 4; 
        FMC_ClearBitState(FMC_FLAG_EOP | FMC_FLAG_WERR | FMC_FLAG_PERR );    
    }
    
  //  FMC_Lock();
    
  return 0;
}

void EE_ReadData(uint16_t VirtAddress, uint8_t *str,uint16_t len)
{
   uint32_t i;
   uint16_t temp;
   //uint32_t Address = EEADDRESS;
   for(i = 0;i<len;i++)
   {
      temp = (*(__IO uint8_t*)(EEADDRESS+i));
      *str = temp;
      str++;
   }
   
  
}

#endif

/**
  * @}
  */ 

/******************* (C) COPYRIGHT 2009 STMicroelectronics *****END OF FILE****/
